/**
 * @api post model.
 *
 * User: yunshengzhu
 * Date: 2020/12/4
 * Time: 下午3:03
 */
package model

import "pkg/cfg"

func CreateOrder(obj OrderDb) error {
	return db.Create(&obj).Error
}

func CreateProOrder(obj OrderDb, list []OrderDetailDb) error {
	tx := db.Begin()
	err := tx.Create(&obj).Error
	if err != nil {
		cfg.LogErr("err:", err)
		tx.Rollback()
		return err
	}

	err = tx.Create(&list).Error
	if err != nil {
		cfg.LogErr("err:", err)
		tx.Rollback()
		return err
	}

	tx.Commit()
	return nil
}

func AddOrderLog(log OrderLogModel) error {
	return db.Create(&log).Error
}

func GetOrderNumber(orderNumber string) (obj OrderDb, err error) {
	return obj, db.Where("order_number=?", orderNumber).Take(&obj).Error
}

func GetOrderQrCode(qrCode string) (obj OrderDb, err error) {
	return obj, db.Where("qr_code=?", qrCode).Take(&obj).Error
}

func UpdateOrder(order OrderDb) error {
	return db.Select("pay_time,pay_status,status,express_company,waybill_number,confirm_time").Where("order_number=?", order.OrderNumber).Updates(order).Error
}

//更新订单号
func UpdateOrderPayAmount(orderNumber string, payAmount uint64) error {
	tx := db.Begin()
	err := tx.Exec(`update t_order set pay_amount=?,order_number=?,wx='' where order_number=?`, payAmount, orderNumber+"0", orderNumber).Error
	if err != nil {
		tx.Rollback()
		return err
	}
	err = tx.Exec(`update t_order_detail set order_number=? where order_number=?`, orderNumber+"0", orderNumber).Error
	if err != nil {
		tx.Rollback()
		return err
	}
	tx.Commit()
	return nil
}

func GetOrderUserIdList(userId int64, status, qrStatus, typ, page, size int) (list []OrderDb, err error) {
	list = make([]OrderDb, 0)
	tx := db
	tx = tx.Where("user_id=?", userId)
	if status >= 0 {
		tx = tx.Where("status=?", status)
	}
	if qrStatus >= 0 {
		tx = tx.Where("qr_status=?", qrStatus)
	}

	if typ > 0 {
		tx = tx.Where("typ=?", typ)
	}

	return list, tx.Offset((page - 1) * size).Limit(size).Order("create_time desc").Find(&list).Error
}

func GetOrderUserIdAndOrderNumber(userId int64, orderNumber string) (obj OrderDb, err error) {
	return obj, db.Where("user_id=? and order_number=?", userId, orderNumber).Take(&obj).Error
}

func GetOrderUserIdCount(userId int64, status int, qrStatus, typ int) (n int64, err error) {
	count := OrderCountDb{}
	tx := db
	tx = tx.Where("user_id=?", userId)
	if status >= 0 {
		tx = tx.Where("status=?", status)
	}

	if qrStatus >= 0 {
		tx = tx.Where("qr_status=?", qrStatus)
	}

	if typ > 0 {
		tx = tx.Where("typ=?", typ)
	}

	return count.Count, tx.Select("count(*) as count").Take(&count).Error
}

func GetOrderDetailList(orderNumber string) (list []OrderDetailDb, err error) {
	list = make([]OrderDetailDb, 0)
	return list, db.Where("order_number=?", orderNumber).Order("id asc").Find(&list).Error
}

func UpdateOrderQr(order OrderDb) error {
	return db.Select(`qr_code,qr_time,exp_qr_time,qr_status,status`).Where("order_number=?", order.OrderNumber).Updates(&order).Error
}

func UpdateOrderWx(orderNumber, wx string) error {
	return db.Exec(`update t_order set wx=? where order_number=?`, wx, orderNumber).Error
}

func GetOrderMidList(mid int64, status int, orderNumber string, typ, page, size int) (list []OrderDb, err error) {
	list = make([]OrderDb, 0)
	tx := db
	tx = tx.Where("mid=?", mid)
	if status >= 0 {
		tx = tx.Where("status=?", status)
	}
	if len(orderNumber) > 0 {
		tx = tx.Where("order_number like ?", "%"+orderNumber+"%")
	}

	if typ == 2 {
		tx = tx.Where("typ=2")
	}

	if typ == 3 {
		tx = tx.Where("typ=3")
	}

	return list, tx.Offset((page - 1) * size).Limit(size).Order("create_time desc").Find(&list).Error
}

func GetOrderMidCount(mid int64, status int, orderNumber string, typ int) (n int64, err error) {
	count := OrderCountDb{}
	tx := db
	tx = tx.Where("mid=?", mid)
	if status >= 0 {
		tx = tx.Where("status=?", status)
	}
	if typ == 2 {
		tx = tx.Where("typ=2")
	}

	if typ == 3 {
		tx = tx.Where("typ=3")
	}

	if len(orderNumber) > 0 {
		tx = tx.Where("order_number like ?", "%"+orderNumber+"%")
	}
	return count.Count, tx.Select("count(*) as count").Take(&count).Error
}
